<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/21
 * Time: 9:36
 */

namespace app\index\server;

use app\index\model\BannerItem as BannerItemModel;
use app\index\model\Image as ImageModel;
use app\lib\exception\DatabaseException;
use think\Db;

class BannerItemServer extends BaseServer
{
    public function getList($params = [])
    {
        // 参数的准备
        $prep_params = $this->prep_param($params);
        try {
            // 总条数
            $count = BannerItemModel::count();
            $banner_list = BannerItemModel::with(['img', 'banner'])
                ->order($prep_params['sort'], $prep_params['order'])
                ->limit($prep_params['offset'], $prep_params['limit'])
                ->select();
        } catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return bootstrap_table_return($count, $banner_list);
    }

    public function addOrUpdate($params = [])
    {
        Db::startTrans();
        try{
            // 返回Image表插入数据的ID
            $image_data = $this->handle_image($params['alias'], $params['url'], 'theme', isset($params['primary_key']), '', 'image');
            $bannerItem_data = [
                'banner_id' => $params['banner_id'],
                'type' => $params['type'],
                'key_word' => $params['key_word'],
                'status' => $params['status']
            ];
            $bannerItem_data['img_id'] = $image_data['img_id'];
            if(isset($params['primary_key'])){
                $result = BannerItemModel::where('id', '=', $params['primary_key'])
                    ->update($bannerItem_data);
            }else{
                $result = BannerItemModel::insert($bannerItem_data);
            }
            if(!$result){
                return setMsg(0, '操作banner图失败');
            }
            Db::commit();
        }catch(Exception $e){
            Db::rollback();
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '操作banner图成功');
    }

    public function detail($id)
    {
        try{
            $detail = BannerItemModel::with(['img'])->find($id);
            if(!$detail){
                return setMsg(0, '查询banner图失败');
            }
        }catch(Exception $e){
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '查询banner图成功', $detail);
    }

    public function changeStatus($id)
    {
        try{
            $info = BannerItemModel::where('id', $id)->find();
            $info->status = abs( ($info->status) - 1 );
            $result = $info->save();
            if(!$result){
                return setMsg(0, '修改banner图状态失败');
            }
        }catch(Exception $e){
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '修改banner图状态成功');
    }

    // 验证图片名是否存在
    protected function check_unique($alias, $id = '')
    {
        if(isset($id)) {
            $result = ImageModel::where('id', '<>', $id)
                ->where('alias', '=', $alias)
                ->find();
        } else {
            $result = ImageModel::where('alias', '=', $alias)
                ->find();
        }
        return $result;
    }
}